package com.org.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.org.common.PageBean;
import com.org.common.Result;
import com.org.pojo.Collect;
import com.org.pojo.CommunityActivityRespon;
import com.org.pojo.CommunityBanner;
import com.org.service.ICommunityBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cjx
 * @since 2024-03-03
 */
@RestController
@RequestMapping("/community-banner")
public class CommunityBannerController {

    @Autowired
    private ICommunityBannerService communityBannerService;

    // 增
    @PostMapping("/add")
    public Result addCommunityBanner(@RequestBody CommunityBanner communityBanner){
        return communityBannerService.save(communityBanner)? Result.success(communityBanner):Result.error("新增横幅失败");
    }

    // 删
    @GetMapping("/delete")
    public Result deleteCommunityBanner(long id){
        return communityBannerService.removeById(id)? Result.success("删除成功"):Result.error("删除横幅失败");
    }

    // 改
    @PostMapping("/update")
    public Result updateCommunityBanner(@RequestBody CommunityBanner communityBanner){
        return communityBannerService.updateById(communityBanner)? Result.success(communityBanner):Result.error("修改横幅失败");
    }

    // 查
    @GetMapping("/detail")
    public Result getCommunityBanner(long id){
        return Result.success(communityBannerService.getById(id));
    }

    // 查所有
    @GetMapping("/listAll")
    public Result< PageBean<CommunityBanner>> getAllCommunityBanners(Integer communityId){
        PageBean<CommunityBanner> pageBean = new PageBean<>();
        List<CommunityBanner> list = communityBannerService.list(new LambdaQueryWrapper<CommunityBanner>().eq(CommunityBanner::getCommunityId, communityId));
        pageBean.setTotal((long) list.size());
        pageBean.setItems(list);
        return Result.success(pageBean);
    }


    // 分页
    @GetMapping("/pageList")
    public Result<PageBean<CommunityBanner>> getCommunityBanners(
            Integer pageNum,    //第几页
            Integer pageSize,   //页面容量
            String communityID  //社区id，可不传，可为空
    ){
        Page<CommunityBanner> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);

        LambdaQueryWrapper<CommunityBanner> queryWrapper = new LambdaQueryWrapper<>();

        if(communityID != null && !communityID.isEmpty()){
            queryWrapper.eq(CommunityBanner::getCommunityId, communityID);
        }

        IPage<CommunityBanner> iPage = communityBannerService.page(page, queryWrapper);

        PageBean<CommunityBanner> pageBean = new PageBean<>();
        pageBean.setTotal(iPage.getTotal());
        pageBean.setItems(iPage.getRecords());

        return Result.success(pageBean);
    }

}
